package com.mdd.admin.service.jiaz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.jiaz.ITimeSlotService;
import com.mdd.admin.validate.commons.PageValidate;

import com.mdd.admin.validate.setting.SettingWebsiteValidate;
import com.mdd.admin.vo.setting.SettingWebsiteVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.TimeSlot;
import com.mdd.common.mapper.TimeSlotMapper;
import com.mdd.common.util.ConfigUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.validate.timeslot.TimeSlotCreateValidate;
import com.mdd.common.validate.timeslot.TimeSlotSearchValidate;
import com.mdd.common.validate.timeslot.TimeSlotUpdateValidate;
import com.mdd.common.vo.timeslot.TimeSlotDetailVo;
import com.mdd.common.vo.timeslot.TimeSlotListedVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 时间段实现类
 *
 * @author LikeAdmin
 */
@Service
public class TimeSlotServiceImpl implements ITimeSlotService {

    @Resource
    TimeSlotMapper timeSlotMapper;

    /**
     * 时间段列表
     *
     * @param pageValidate   分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TimeSlotListedVo>
     * @author LikeAdmin
     */
    @Override
    public PageResult<TimeSlotListedVo> list(PageValidate pageValidate, TimeSlotSearchValidate searchValidate) {
        Integer page = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<TimeSlot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        timeSlotMapper.setSearch(queryWrapper, searchValidate, new String[]{"=:isDisable@is_disable:long",});

        IPage<TimeSlot> iPage = timeSlotMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TimeSlotListedVo> list = new LinkedList<>();
        for (TimeSlot item : iPage.getRecords()) {
            TimeSlotListedVo vo = new TimeSlotListedVo();
            BeanUtils.copyProperties(item, vo);
            if (StringUtils.isNotBlank(vo.getStartTime())) {
                vo.setStartTime(vo.getStartTime().substring(0, 5));
            }
            if (StringUtils.isNotBlank(vo.getEndTime())) {
                vo.setEndTime(vo.getEndTime().substring(0, 5));
            }
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 时间段详情
     *
     * @param id 主键参数
     * @return TimeSlot
     * @author LikeAdmin
     */
    @Override
    public TimeSlotDetailVo detail(Integer id) {
        TimeSlot model = timeSlotMapper.selectOne(new QueryWrapper<TimeSlot>().eq("id", id).eq("is_delete", 0).last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TimeSlotDetailVo vo = new TimeSlotDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 时间段新增
     *
     * @param createValidate 参数
     * @author LikeAdmin
     */
    @Override
    public void add(TimeSlotCreateValidate createValidate) {
        TimeSlot model = new TimeSlot();
        BeanUtils.copyProperties(createValidate, model);
        model.setCreateTime(TimeUtils.timestamp());
        model.setUpdateTime(TimeUtils.timestamp());
        timeSlotMapper.insert(model);
    }

    /**
     * 时间段编辑
     *
     * @param updateValidate 参数
     * @author LikeAdmin
     */
    @Override
    public void edit(TimeSlotUpdateValidate updateValidate) {
        TimeSlot model = timeSlotMapper.selectOne(new QueryWrapper<TimeSlot>().eq("id", updateValidate.getId()).eq("is_delete", 0).last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        BeanUtils.copyProperties(updateValidate, model);
        model.setUpdateTime(TimeUtils.timestamp());
        timeSlotMapper.updateById(model);
    }

    /**
     * 时间段删除
     *
     * @param id 主键ID
     * @author LikeAdmin
     */
    @Override
    public void del(Integer id) {
        TimeSlot model = timeSlotMapper.selectOne(new QueryWrapper<TimeSlot>().eq("id", id).eq("is_delete", 0).last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        timeSlotMapper.updateById(model);
    }

    @Override
    public SettingWebsiteVo detail() {
        SettingWebsiteVo vo = new SettingWebsiteVo();

        Map<String, String> website = ConfigUtils.get("website");
        String jiazOrderDay = website.getOrDefault("jiazOrderDay", "0");
        String jiazOrderHour = website.getOrDefault("jiazOrderHour", "0");

        vo.setJiazOrderDay(Integer.parseInt(jiazOrderDay));
        vo.setJiazOrderHour(Integer.parseInt(jiazOrderHour));
        return vo;
    }

    @Override
    public void saveTimeSlotDay(SettingWebsiteValidate websiteValidate) {
        ConfigUtils.set("website", "jiazOrderDay", String.valueOf(websiteValidate.getJiazOrderDay()));
        ConfigUtils.set("website", "jiazOrderHour", String.valueOf(websiteValidate.getJiazOrderHour()));
    }

}
